<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包的作用</title>
</head>
<body>
<!--
完成js3 闭包的内容的
-->
<!--
c、可以实现封装，属性私有化  eg：Person()
-->
<script>
    //变量prepareWife私有化的应用
    function Deng(name,wife) {
        var prepareWife = "小张";
        this.wife = wife;
        this.divorce = function () {
            this.wife = prepareWife;
        };
        this.changePrepareWife = function (target) {
            prepareWife = target;
        };
        this.sayPrepareWife = function () {
            console.log(prepareWife);
        }
    }
    var deng = new Deng("deng","xiaoliu");//new 执行，Deng返回this，将divorce、changePrepareWife、sayPrepareWife函数保存到了外部
                          //形成闭包
    console.log(deng);
    //changePrepareWife: ƒ (target)  divorce: ƒ ()  sayPrepareWife: ƒ ()  wife: "xiaoliu"  __proto__: Object
    deng.divorce();//外部执行怎么能用内部的变量？//因为divorce函数被保存到了外部，它储存的Deng函数的执行期上下文
    console.log(deng.wife);//小张 ?  //是divorce()方法用了prepareWife变量

    console.log(deng.prepareWife);//undefined 访问不到
    console.log(deng.sayPrepareWife());//小张

</script>
</body>
</html>
